package cn.hanyx.coder.core.concurrent.thread.demo02;

/**
 * 线程类
 */
class MyThread implements Runnable {
    // 一共5张票
    private int ticket = 5;

    @Override
    public void run() {
        for (int x = 0; x < 100; x++) {
//            System.out.println(Thread.currentThread().getName() +",x=" + x);
            // 第一步是判断
            if (this.ticket > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 第二步修改
                System.out.println(Thread.currentThread().getName()
                        + "卖票，ticket = " + this.ticket--);
            }
        }
    }
}

public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        MyThread mt = new MyThread();
        new Thread(mt, "票贩子A").start();
        new Thread(mt, "票贩子B").start();
        new Thread(mt, "票贩子C").start();
        new Thread(mt, "票贩子D").start();
        new Thread(mt, "票贩子E").start();
    }
}